﻿using System;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Media.Imaging;
using Microsoft.Xna.Framework.Graphics;
using System.Windows.Shapes;


namespace BootyShaker.Utils
{
    public class Util
    {
        public static WriteableBitmap OpenResourceImage(String fileName)
        {

            System.IO.Stream src = Application.GetResourceStream(new Uri(@"/BootyShaker;component/" + fileName, UriKind.Relative)).Stream;
            //IsolatedStorageFileStream dest = new IsolatedStorageFileStream(System.IO.Path.GetFileName(fileName), System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, store);
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(src);
            WriteableBitmap picLibraryImage = new WriteableBitmap(bitmap);

            src.Position = 0;
            //CopyStream(src, dest);
            //dest.Flush();
            //dest.Close();
            src.Close();
            //dest.Dispose();
            return picLibraryImage;

        }

        public static void CopyFromContentToStorage(String fileName)
        {
            using (IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                try
                {

                    System.IO.Stream src = Application.GetResourceStream(new Uri(@"/BootyShaker;component/" + fileName, UriKind.Relative)).Stream;
                    IsolatedStorageFileStream dest = new IsolatedStorageFileStream(System.IO.Path.GetFileName(fileName), System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, myStore);
                    src.Position = 0;
                    CopyStream(src, dest);
                    dest.Flush();
                    dest.Close();
                    src.Close();
                    dest.Dispose();

                }
                catch { }
            }
        }

        private static void CopyStream(System.IO.Stream input, IsolatedStorageFileStream output)
        {
            byte[] buffer = new byte[32768];
            long TempPos = input.Position;
            int readCount;
            do
            {
                readCount = input.Read(buffer, 0, buffer.Length);
                if (readCount > 0) { output.Write(buffer, 0, readCount); }
            } while (readCount > 0);
            input.Position = TempPos;
        }

        public static Rect ControlBounds(FrameworkElement control)
        {
            //System.Windows.Media.GeneralTransform gt = control.TransformToVisual(Application.Current.RootVisual);

            //Point loc = gt.Transform(new Point(System.Windows.Controls.Canvas.GetLeft(control), System.Windows.Controls.Canvas.GetTop(control)));
            Point loc = new Point(0, 0);
            Rect rect = new Rect(loc, new Size(control.ActualWidth, control.ActualHeight));

            return rect;
        }

        public static bool intersects(Rect r, Point p)
        {
            return intersects(r, new Rect(p, new Size(0, 0)));

        }
        public static bool intersects(Rect r, Rect r2)
        {
            r.Intersect(r2);
            return r != Rect.Empty;
        }

        const double Rad2Deg = 180.0 / Math.PI;
        const double Deg2Rad = Math.PI / 180.0;

        /// <summary>
        /// Calculate angle in radians between line defined with two points and x-axis.
        /// </summary>
        public static double angle(Point start, Point end)
        {
            return Math.Atan2(start.Y - end.Y, end.X - start.X) * Rad2Deg;
        }
        //public static double angle(Point p1, Point p2)
        //{

        //    // Negate X and Y values
        //    double pxRes = p2.X - p1.X;

        //    double pyRes = p2.Y - p1.Y;
        //    double angle = 0.0;

        //    // Calculate the angle
        //    if (pxRes == 0.0)
        //    {
        //        if (pxRes == 0.0)

        //            angle = 0.0;
        //        else if (pyRes > 0.0) angle = System.Math.PI / 2.0;

        //        else
        //            angle = System.Math.PI * 3.0 / 2.0;

        //    }
        //    else if (pyRes == 0.0)
        //    {
        //        if (pxRes > 0.0)

        //            angle = 0.0;

        //        else
        //            angle = System.Math.PI;

        //    }

        //    else
        //    {
        //        if (pxRes < 0.0)

        //            angle = System.Math.Atan(pyRes / pxRes) + System.Math.PI;
        //        else if (pyRes < 0.0) angle = System.Math.Atan(pyRes / pxRes) + (2 * System.Math.PI);

        //        else
        //            angle = System.Math.Atan(pyRes / pxRes);

        //    }

        //    // Convert to degrees
        //    angle = angle * 180 / System.Math.PI; return angle;



        //}

        public static float Distance(Point A, Point B)
        {
            return (float)Math.Sqrt(Math.Pow((double)(A.X - B.X), 2) + Math.Pow((double)(A.Y - B.Y), 2));
        }
        public static Point Center(Point A, Point B)
        {
            return new Point((A.X + B.X) / 2, (A.Y + B.Y) / 2);
        }
        public static Texture2D createellipsetexture(Size sz)
        {
            Ellipse ellipse = new Ellipse();

            ellipse.Fill = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red);

            ellipse.StrokeThickness = 1;
            ellipse.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red);
            ellipse.Width = sz.Width;
            ellipse.Height = sz.Height;
            WriteableBitmap texture = new WriteableBitmap((int)ellipse.Width, (int)ellipse.Height);
            texture.Render(ellipse, new System.Windows.Media.TranslateTransform());
            texture.Invalidate();
            Texture2D txt = new Texture2D(Microsoft.Xna.Framework.SharedGraphicsDeviceManager.Current.GraphicsDevice, texture.PixelWidth, texture.PixelHeight);
            txt.SetData(Imaging.BGRAToRGBA(texture));
            return txt;
        }
    }
}
